package com.example.contactapp.providers;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;

import com.example.contactapp.objects.ContactObject;
import com.example.contactapp.objects.SmsObject;
import com.example.contactapp.objects.ThreatObject;

public class SMSThread {
	private Context mContext;
	private static SMSThread instance;
	private HashMap<String, ArrayList<SmsObject>> mAllSmses;
	private Cursor cursor;

	public class TextSmsColumns {
		public static final String ID = "_id";
		public static final String ADDRESS = "address";
		public static final String DATE = "date";
		public static final String BODY = "body";
		public static final String THREAD_ID = "thread_id";
		public static final String PROTOCOL = "protocol";
		public static final String PERSON = "person";
		public static final String STATUS = "status";
		public static final String TYPE = "type";
		public static final String SUBJECT = "subject";
		public static final String READ = "read";
	}

	public static final Uri SMS_INBOX_CONTENT_URI = Uri.parse("content://sms/");

	private SMSThread(Context context) {
		mContext = context;
	}

	public static SMSThread getInstance(Context context) {
		if (instance == null) {
			instance = new SMSThread(context);
		}
		return instance;
	}

	@SuppressWarnings("deprecation")
	public HashMap<String, ArrayList<SmsObject>> getAllSMS() {
		mAllSmses = new HashMap<>();
		cursor = ((Activity) mContext).managedQuery(SMS_INBOX_CONTENT_URI,
				new String[] { TextSmsColumns.ID, TextSmsColumns.ADDRESS,
						TextSmsColumns.DATE, TextSmsColumns.BODY,
						TextSmsColumns.THREAD_ID, TextSmsColumns.PROTOCOL,
						TextSmsColumns.PERSON, TextSmsColumns.STATUS,
						TextSmsColumns.TYPE, TextSmsColumns.SUBJECT,
						TextSmsColumns.READ }, null, null, null);
		SmsObject object = null;
		if (cursor.moveToFirst()) {
			do {
				object = new SmsObject();

				String address = cursor.getString(cursor
						.getColumnIndex(TextSmsColumns.ADDRESS));
				ContactObject ct = getContact(mContext, address);
				object.setTime(cursor.getLong(cursor
						.getColumnIndex(TextSmsColumns.DATE)));
				object.setStrTime(getDate(cursor.getLong(cursor
						.getColumnIndex(TextSmsColumns.DATE))));
				object.setBodySms(cursor.getString(cursor
						.getColumnIndex(TextSmsColumns.BODY)));
				object.setAddress(address);
				object.setName(ct.getName());
				object.setAvatar(ct.getAvatar());
				object.setThreadId(cursor.getInt(cursor
						.getColumnIndex(TextSmsColumns.THREAD_ID)));
				object.setType(cursor.getInt(cursor
						.getColumnIndex(TextSmsColumns.TYPE)));
				
				if(mAllSmses.containsKey(ct.getName())){
					mAllSmses.get(ct.getName()).add(0, object);
				}else{
					ArrayList<SmsObject> objects = new ArrayList<>();
					objects.add(object);
					mAllSmses.put(ct.getName(), objects);
				}
			} while (cursor.moveToNext());
		}

		
		
		return mAllSmses;
	}

	public ContactObject getContact(Context context, String phoneNumber) {
		ContactObject object = new ContactObject();
		ContentResolver cr = context.getContentResolver();
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(phoneNumber));
		Cursor cursor = cr.query(uri, new String[] { PhoneLookup.DISPLAY_NAME,
				PhoneLookup.PHOTO_URI }, null, null, null);
		if (cursor == null) {
			return null;
		}
		if (cursor.moveToFirst()) {
			object.setName(cursor.getString(cursor
					.getColumnIndex(PhoneLookup.DISPLAY_NAME)));
			object.setAvatar(cursor.getString(cursor
					.getColumnIndex(PhoneLookup.PHOTO_URI)));

		} else {
			object.setName(phoneNumber);
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return object;
	}
	
	public String getDate(long milliseconds) {
		Date d = new Date(milliseconds);
		Calendar calNow = Calendar.getInstance();
		Calendar calYesterday = Calendar.getInstance();
		calNow.add(Calendar.DATE, -1);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

		String dateNow = dateFormat.format(calNow.getTime());
		String dateYesterday = dateFormat.format(calYesterday.getTime());
		String dateLog = dateFormat.format(d.getTime());

		if (dateLog.equals(dateNow))
			return getHours(milliseconds);
		else if (dateLog.compareTo(dateYesterday) == 0)
			return "Yesterday";
		Date date = new Date(milliseconds);
		SimpleDateFormat f = new SimpleDateFormat("MMM dd");
		return f.format(date);
	}

	public String getHours(long milliseconds) {
		Date date = new Date(milliseconds);
		SimpleDateFormat f = new SimpleDateFormat("HH:MM");
		return f.format(date);
	}
}
